import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";
// import vitePluginImport from "vite-plugin-babel-import";
// import fs from "fs";
// import lessToJS from "less-vars-to-js"; //less-var-to-js是将less样式转化为json键值对的形式
const path = require("path");
import config from "./src/config";

// const themeVariables = lessToJS(fs.readFileSync(path.resolve(__dirname, "./src/config/variables.less"), "utf8"));
const env = process.argv[process.argv.length - 1];
const base = config[env];

export default defineConfig({
    base: base.cdn,
    plugins: [reactRefresh()],
    define: {
        "process.env": {},
    },
    css: {
        preprocessorOptions: {
            less: {
                // 支持内联 JavaScript
                javascriptEnabled: true,
                // 自动导入全局样式
                additionalData: "@import '@/assets/styles/base.less';",
            },
        },
    },
    resolve: {
        alias: [
            {
                find: /^~/,
                replacement: "",
            },
            {
                find: "@",
                replacement: path.resolve(__dirname, "src"),
            },
            {
                find: "components",
                replacement: path.resolve(__dirname, "src/components"),
            },
            {
                find: "pages",
                replacement: path.resolve(__dirname, "src/pages"),
            },
            {
                find: "store",
                replacement: path.resolve(__dirname, "src/store"),
            },
            {
                find: "assets",
                replacement: path.resolve(__dirname, "src/assets"),
            },
            {
                find: "network",
                replacement: path.resolve(__dirname, "src/network"),
            },
            {
                find: "utils",
                replacement: path.resolve(__dirname, "src/utils"),
            },
            {
                find: "router",
                replacement: path.resolve(__dirname, "src/router"),
            },
        ],
    },
    server: {
        proxy: {
            "/apis": {
                target: "https://srv.hotkidceo.com/appapi",
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/apis/, ""),
            },
        },
        port: 3001, //开发环境启动的端口
    },
});
